$(function(){
	
	var baseUrl = $("base").attr("href");
	var pageSize = 9;
	var curPage = 1;
	
	
	
	
	
	/** 加载图片 */
	function loadImg($_node){
		var publicInfo = $_node.data("publicInfo");
		console.log("initImg", publicInfo);
		$.ajax({
			url : baseUrl + "commenUtil/json-getListFile.do",
			method : "post",
			data : {type : "描述文件", estateInfoId : publicInfo.estatebasicinfo.id},
			success : function(data){
				console.log("getFileData", data);
				if(data.code == 1000){
					console.log("fileData", data)
					if((typeof data.data[0]) != "undefined" && data.data[(data.data.length-1)].path != "")
						$_node.find("img").attr("src", data.data[(data.data.length-1)].path);
				}
			},
			
		});
	}
	
	/** 加载发布的房产信息  */
	function loadPublicInfo(){
		$("#public-list").find(".showList").remove();
		$.ajax({
			url : baseUrl + "user/getPublicInfo.do",
			data : {pageSize : pageSize, pageNum : curPage},
			method : "post",
			success : function(data){
				
				
				
				if(data.code == 1000){
					var publicList = JSON.parse(data.data);
					console.log("publicList", publicList);
					var $_Node;
					if(publicList.length == 0){
						$(".next").addClass("disabled");
						curPage--;
					}else if( publicList.length < pageSize ){
						$(".next").addClass("disabled");
					}else{
						$(".next").removeClass("disabled");
					}
					for (var i = 0; i < publicList.length; i++) {
						if((i % 3) == 0 ){
							$("#public-list").append(
								"<tr class=\"showList\">"+
								"    <td>"+
								"   </td>"+
								"</tr>");
							
							$_Node = $("#public-list").find(".showList").last().find("td");
							
							
						}
						if( (i % 3) < 2){
							$_Node.append(
								"<table align=\"left\" width=\"215\" style=\"text-align:center\" class=\"trends\">"+
								"<tr>"+
								"<td>"+
								"<img src=\"Content/img/showPublic/default-house.jpg\" >"+
								"</td>"+
								"</tr>"+
								"<tr>"+
								"<td class=\"public-info-title\">"+
								publicList[i].estatebasicinfo.estateType+
								"</td>"+
								"</tr>"+
								"<tr>"+
								"<td class=\"public-info-describe\">"+
								"<span class=\"acreage\">"+
								"	"+ publicList[i].estatebasicinfo.acreage +
								"</span>&nbsp;m<sup>2</sup> &nbsp;&nbsp;￥<span class=\"price\">"+
								"	"+ publicList[i].minPrice +
								"</span>&nbsp;/&nbsp;<span  class=\"unit\">"+
								"	"+ publicList[i].unit +
								"</span>"+
								"</td>"+
								"</tr>"+
								"<tr>"+
								"<td height=\"20\"></td>"+
								"</tr>"+
								"<tr>"+
								"<td>"+
								"<a href=\"user/getPublicInfoDetailViewById.do?id="+ publicList[i].id +"\" class=\"pubic-info-btn\">查看详情</a>"+
								"</td>"+
								"</tr>"+
								"</table>"+
								"<table align=\"left\" class=\"member\">"+
								"<tbody>"+
								"<tr>"+
								"<td width=\"20\"></td>"+
								"</tr>"+
								"</tbody>"+
								"</table>"
							);
						}else{
							$_Node.append(
								"<table align=\"left\" width=\"215\" style=\"text-align:center\" class=\"trends\">"+
								"<tr>"+
								"<td>"+
								"<img src=\"Content/img/showPublic/default-house.jpg\" >"+
								"</td>"+
								"</tr>"+
								"<tr>"+
								"<td class=\"public-info-title\">"+
								publicList[i].estatebasicinfo.estateType+
								"</td>"+
								"</tr>"+
								"<tr>"+
								"<td class=\"public-info-describe\">"+
								"<span class=\"acreage\">"+
								"	"+ publicList[i].estatebasicinfo.acreage +
								"</span>&nbsp;m<sup>2</sup> &nbsp;&nbsp;￥<span class=\"price\">"+
								"	"+ publicList[i].minPrice +
								"</span>&nbsp;/&nbsp;<span  class=\"unit\">"+
								"	"+ publicList[i].unit +
								"</span>"+
								"</td>"+
								"</tr>"+
								"<tr>"+
								"<td height=\"20\"></td>"+
								"</tr>"+
								"<tr>"+
								"<td>"+
								"<a href=\"user/getPublicInfoDetailViewById.do?id="+ publicList[i].id +"\" class=\"pubic-info-btn\">查看详情</a>"+
								"</td>"+
								"</tr>"+
								"</table>"
								);
						}
						
						$_Node.find(".trends").last().data("publicInfo",publicList[i]);
						loadImg($_Node.find(".trends").last());
					}
					
					
					
					
					
				}else{
					sweetAlert({
						title:"加载数据失败",
						text:"系统错误",
						type:"error",
						timer:2000,
					});
					console.log("loadInfoError",data);
				}
			},
			erorr : function(){
				sweetAlert({
						title:"加载数据失败",
						text:"连接服务器失败",
						type:"error",
						timer:2000,
					});
			}
			
		});
	}
	
	
	
	loadPublicInfo();
	
	
	
	/**翻页**/
	
	$(".previous").click(function(){
		curPage--;
		if(curPage < 1){
			curPage = 1;
			$(this).addClass("disabled")
			return;
		}
		loadPublicInfo();
		
	});
	
	$(".next").click(function(){
		curPage++;
		if(curPage == 2){
			$(".previous").removeClass("disabled");
		}
		loadPublicInfo();
	});
	

	
	
});